package com.socialvampic.login;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.social.vampic.R;
import com.socialvampic.mvc.view.AbstractView;

public class RegisterView extends AbstractView {

	private Button mRegisterButton;
	private Button mCancelButton;
	private EditText mEmailEditText;
	private EditText mAccountEditText;
	private EditText mPasswordEditText;
	private EditText mConfirmEditText;

	private OnRegisterListener mRegisterListener;

	public RegisterView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public RegisterView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createView(LayoutInflater inflater, ViewGroup container) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.register_screen, this);

		mRegisterButton = (Button) view.findViewById(R.id.register_ok_button);
		mCancelButton = (Button) view.findViewById(R.id.register_cancel_button);
		mEmailEditText = (EditText) view
				.findViewById(R.id.register_email_edittext);
		mAccountEditText = (EditText) view
				.findViewById(R.id.register_account_edittext);
		mPasswordEditText = (EditText) view
				.findViewById(R.id.register_password_edittext);
		mConfirmEditText = (EditText) view
				.findViewById(R.id.register_passwordconfirm_edittext);

		mRegisterButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mRegisterListener != null) {
					final String email = mEmailEditText.getEditableText()
							.toString();
					final String account = mAccountEditText.getEditableText()
							.toString();
					final String password = mPasswordEditText.getEditableText()
							.toString();
					final String confirm = mConfirmEditText.getEditableText()
							.toString();

					mRegisterListener.OnRegister(email, account, password,
							confirm);
				}

			}
		});

		mCancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mRegisterListener != null) {
					mRegisterListener.OnCancel();
				}
			}
		});
	}

	@Override
	public void reloadView() {
		// TODO Auto-generated method stub
		mCancelButton.setText(R.string.sign_in_cancel_button);
		mRegisterButton.setText(R.string.register_ok_button);
	}

	@Override
	public void destroyView() {
		// TODO Auto-generated method stub
		super.destroyView();
		removeAllViews();
		setRegisterListener(null);
		mCancelButton.setOnClickListener(null);
		mRegisterButton.setOnClickListener(null);
	}

	public void setRegisterListener(OnRegisterListener listener) {
		mRegisterListener = listener;
	}

	// Handle event on register screen
	public static interface OnRegisterListener {
		public void OnCancel();

		public void OnRegister(String email, String account, String password,
				String confirmPassword);
	}
}
